<template>
  <div class="wrap">
    <a-row :gutter="16">
      <a-col :span="6" class="top-col" v-for="item in cardData" :key="item.title">
        <div class="top-card">
          <div class="card-left" :style="`background: ${item.color}`">
            <SvgIcon :name="item.icon" class="left-icon" />
          </div>
          <div class="card-right">
            <span class="right-num">{{ item.num }}</span>
            <span class="right-title">{{ item.title }}</span>
          </div>
        </div>
      </a-col>
    </a-row>
    <a-row :gutter="16">
      <a-col :span="8">
        <a-row class="column">
          <a-col :span="24" class="col-1">
            <div class="clo-1-row clo-1-row-1">
              <div class="weather-box">
                <div class="weather-top">
                  <div>{{ state.weather.data.city }}</div>
                  <div>{{ state.weather.data.realtime.temperature }}℃ {{ state.weather.data.realtime.info }}</div>
                </div>
                <div class="weather-bottom">
                  <div class="info">
                    <div>湿度</div>
                    <div>{{ state.weather.data.realtime.humidity }} %</div>
                  </div>
                  <div class="line"></div>
                  <div class="info">
                    <div>风力</div>
                    <div>{{ state.weather.data.realtime.power }}-{{ state.weather.data.realtime.direct }}</div>
                  </div>
                  <div class="line"></div>
                  <div class="info">
                    <div>空气质量</div>
                    <div>{{ state.weather.data.realtime.aqi }}</div>
                  </div>
                </div>
              </div>
            </div>
          </a-col>
          <a-col :span="24" class="col-1">
            <div class="clo-1-row clo-1-row-2" id="c1"></div>
            <div id="c1-bottom" class="clo-1-row-2-bottom">￥ {{ state.todaySalesTotal }}</div>
          </a-col>
          <a-col :span="24" class="col-1">
            <div class="clo-1-row clo-1-row-2" id="c2"></div>
            <div id="c2-bottom" class="clo-1-row-2-bottom">￥ {{ state.monthSalesTotal }}</div>
          </a-col>
        </a-row>
      </a-col>
      <a-col :span="16">
        <a-row class="column" :gutter="16">
          <a-col :span="12" class="col-2">
            <div class="order clo-2-row clo-2-row-1">
              <div class="order-header">
                <span class="anticon"><SvgIcon name="barchart" /></span>
                <span class="order-title">待办订单</span>
                <span class="order-date">{{ new Date().toLocaleDateString() }}</span>
              </div>
              <a-card size="small" class="order-card">
                <div class="col-1 order-col">
                  <a-timeline-item color="gray">
                    <p>待发货</p>
                    <div class="order-col-content">
                      <div class="order-content-left">
                        <SvgIcon name="wallet" />
                      </div>
                      <div class="order-content-center">
                        <p class="order-content-center-title">订单数: 378 单</p>
                        <p>总金额 22,987.00</p>
                      </div>
                      <div class="order-content-right">
                        <a-button size="small" shape="round">去处理</a-button>
                      </div>
                    </div>
                  </a-timeline-item>
                  <a-timeline-item color="gray">
                    <p>已发货</p>
                    <div class="order-col-content">
                      <div class="order-content-left">
                        <SvgIcon name="car-fill" />
                      </div>
                      <div class="order-content-center">
                        <p class="order-content-center-title">订单数: 299 单</p>
                        <p>总金额 12,987.00</p>
                      </div>
                    </div>
                  </a-timeline-item>
                  <a-timeline-item color="gray">
                    <p>待付款</p>
                    <div class="order-col-content">
                      <div class="order-content-left">
                        <SvgIcon name="creditcard-fill" />
                      </div>
                      <div class="order-content-center">
                        <p class="order-content-center-title">订单数: 199 单</p>
                        <p>总金额 5,987.00</p>
                      </div>
                    </div>
                  </a-timeline-item>
                </div>
              </a-card>
            </div>
          </a-col>
          <a-col :span="12" class="col-2">
            <div class="clo-2-row clo-2-row-2">
              <div class="log-header">
                <p class="log-title-zn">操作日志</p>
                <p class="log-title-en">
                  Operation Log
                </p>
              </div>
              <a-card size="small" class="log-card">
                <ul class="log-list" v-if="state.operationLog.data.length > 0">
                  <li
                    v-for="(item, index) in state.operationLog.data"
                    :key="item.admin_operation_log_id"
                    class="log-item"
                  >
                    {{ index + 1 }}&nbsp;&nbsp;&nbsp;&nbsp;{{
                      `${item.create_time}&nbsp;&nbsp;&nbsp;&nbsp; 用户: ${item.AdminUser.username}&nbsp;&nbsp;&nbsp;&nbsp; 访问了: ${item.AdminMenu.name} `
                    }}
                  </li>
                </ul>
              </a-card>
            </div>
          </a-col>
          <a-col :span="24" class="col-2"><div class="clo-2-row clo-2-row-2" id="c3"></div></a-col>
        </a-row>
      </a-col>
    </a-row>
  </div>
</template>

<script>
import { ref, reactive, getCurrentInstance, onMounted } from 'vue';
import { Line, Column } from '@antv/g2plot';
export default {
  name: 'Home',
  setup() {
    const { ctx } = getCurrentInstance();

    let cardData = ref([
      {
        icon: 'user',
        num: 0,
        title: 'New Users',
        color: '#6ccac9'
      },
      {
        icon: 'tags',
        num: 0,
        title: 'Sales',
        color: '#ff6c60'
      },
      {
        icon: 'shopping',
        num: 0,
        title: 'New Order',
        color: '#f8d347'
      },
      {
        icon: 'barchart',
        num: 0,
        title: 'Total Profit',
        color: '#57c8f2'
      }
    ]);

    let state = reactive({
      weather: {
        data: {
          realtime: {},
          future: {}
        }
      },
      todaySales: [],
      monthSales: [],
      activeUsers: [],
      operationLog: {
        data: []
      }
    });

    async function getData() {
      let res = await ctx.$fetch.post('/manager.index/index');
      state.weather = res.data.Weather;
      state.operationLog = res.data.operation_log;
      cardData.value[0].num = res.data.new_users;
      cardData.value[1].num = res.data.sales;
      cardData.value[2].num = res.data.new_order;
      cardData.value[3].num = res.data.total_profit;
    }
    getData();

    async function getCharts() {
      let getTodaySales = ctx.$fetch.post('/manager.index/todaySales');
      let getMonthSales = ctx.$fetch.post('/manager.index/monthSales');
      let getActiveUsers = ctx.$fetch.post('/manager.index/activeUsers');
      let todaySales = await getTodaySales;
      let monthSales = await getMonthSales;
      let activeUsers = await getActiveUsers;
      state.monthSales = monthSales.data.data;
      state.monthSalesTotal = monthSales.data.total;
      state.todaySales = todaySales.data.data;
      state.todaySalesTotal = todaySales.data.total;
      state.activeUsers = activeUsers.data;
      chartInit1();
      chartInit2();
      chartInit3();
    }
    getCharts();

    function chartInit1() {
      const chart = new Line('c1', {
        title: {
          visible: true,
          text: '今日收入',
          style: {
            fontSize: 18,
            fill: 'white'
          }
        },
        description: {
          visible: true,
          style: {
            fontSize: 14,
            fill: 'white'
          },
          text: (() => {
            let total = 0;
            state.todaySales.forEach((t) => {
              total += t.value;
            });
            return '￥' + total;
          })()
        },
        forceFit: true,
        data: state.todaySales.map((t) => {
          return {
            time: t.time,
            value: Number(t.value)
          };
        }),
        xField: 'time',
        yField: 'value',
        point: {
          visible: true,
          shape: 'circle',
          size: 3,
          style: {
            fill: '#fff'
          }
        },
        xAxis: {
          visible: false
        },
        yAxis: {
          visible: false
        },
        lineStyle: {
          stroke: '#fff'
        },
        meta: {
          time: {
            alias: '时间'
          },
          value: {
            alias: '收入'
          }
        }
      });
      chart.render();
    }
    function chartInit2() {
      const chart = new Column('c2', {
        title: {
          visible: true,
          text: '总营收',
          style: {
            fontSize: 18,
            fill: 'white'
          }
        },
        description: {
          visible: true,
          style: {
            fontSize: 14,
            fill: 'white'
          },
          text: (() => {
            let total = 0;
            state.monthSales.forEach((t) => {
              total += Number(t.active_number);
            });
            return '￥' + total;
          })()
        },
        forceFit: true,
        data: state.monthSales.map((t) => {
          return {
            date: t.date,
            active_number: Number(t.active_number)
          };
        }),
        xField: 'date',
        yField: 'active_number',
        xAxis: {
          visible: false
        },
        yAxis: {
          visible: false
        },
        columnStyle: {
          fill: '#fff'
        },
        columnSize: 12,
        meta: {
          date: {
            alias: '日期'
          },
          active_number: {
            alias: '营收'
          }
        }
      });
      chart.render();
    }
    function chartInit3() {
      const chart = new Column('c3', {
        title: {
          visible: true,
          text: '用户活跃度',
          style: {
            fontSize: 18,
            fill: '#6a6470'
          }
        },
        forceFit: true,
        data: state.activeUsers.map((t) => {
          return {
            date: t.date,
            active_number: Number(t.active_number)
          };
        }),
        xField: 'date',
        yField: 'active_number',
        xAxis: {
          visible: false
        },
        yAxis: {
          visible: false
        },
        columnStyle: {
          fill: '#bfc2cd'
        },
        meta: {
          date: {
            alias: '日期'
          },
          active_number: {
            alias: '活跃度'
          }
        },
        guideLine: [
          {
            type: 'max', // 'max' | 'min' | 'median' |  'mean'
            lineStyle: {
              stroke: '#ccc',
              lineDash: [4, 2]
            }
          }
        ]
      });
      chart.render();
    }

    onMounted(() => {
      // chartInit();
    });

    return {
      cardData,
      state
    };
  }
};
</script>
<style lang="scss" scoped>
.order {
  line-height: 14px;
  color: #fff;
  border-radius: 5px 5px 0;
  .order-header {
    background: #ff6c60;
  }
  .anticon {
    padding: 10px;
    font-size: 24px;
    background: #e56155;
    border-top-left-radius: 5px;
  }
  .order-title {
    padding-left: 12px;
    font-size: 16px;
    font-weight: 400;
  }
  .order-date {
    float: right;
    padding: 15px 4px;
    font-size: 14px;
    background: #e56155;
    border-top-right-radius: 5px;
  }
  .clo-2-row {
    height: 100px;
  }
  .order-col-content {
    width: 100%;
    height: 100%;
    font-weight: 400;
  }
  .order-content-left {
    display: inline-block;
    width: 14%;
    height: 100%;
    padding: 0;
    margin: 0;
    font-size: 46px;
    line-height: 10px;
  }
  .order-content-center {
    display: inline-block;
    width: 56%;
    height: 100%;
    font-size: 12px;
    line-height: 26px;
    text-align: left;
    .order-content-center-title {
      font-size: 16px;
      font-weight: bold;
    }
  }
  .order-content-right {
    display: inline-block;
    width: 18%;
    height: 100%;
    font-size: 12px;
  }
}

.log-item {
  padding: 10px;
  font-size: 14px;
  font-weight: 500;
  color: #808284;
  border-bottom: 1px solid #f6f7f8;
}
.log-item:hover {
  padding: 10px;
  background: #f7f8fc;
}
.wrap {
  height: 100%;
  padding: 10px;
  background-color: #f1f2f7;
  .top-card {
    display: flex;
    height: 100px;
    overflow: hidden;
    border-radius: 4px;
    &:hover {
      box-shadow: 0 0 8px 2px #ccc;
      .card-left {
        opacity: 0.9;
        .left-icon {
          font-size: 80px;
        }
      }
    }
  }
  .card-left {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 35%;
    .left-icon {
      font-size: 70px;
      color: #fff;
    }
  }
  .card-right {
    display: flex;
    flex: 1;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: #c6cad6;
    background: #fff;
    .right-num {
      font-size: 40px;
      line-height: 1;
    }
    .right-title {
      font-size: 16px;
    }
  }
}
.column {
  height: calc(100vh - 170px);
  padding-top: 10px;
  padding-bottom: 10px;
  .col-1 {
    height: 33%;
    padding-bottom: 10px;
  }
  .clo-1-row {
    height: 95%;
  }
  .clo-1-row-2 {
    height: 72%;
  }
  .clo-1-row-2-bottom {
    display: flex;
    flex-direction: row-reverse;
    align-items: center;
    height: 24%;
    padding-right: 16px;
    font-size: 20px;
    line-height: 54px;
    color: #b5e378;
    text-align: right;
    background-color: #99c262;
    border-radius: 0 0 4px 4px;
  }
  .col-2 {
    height: 50%;
  }
  .clo-2-row {
    height: 100%;
  }
  .order-card {
    height: calc(100% - 44px);
    overflow: auto;
  }
}

#c1 {
  padding-bottom: 30px;
  background-color: #41cac0;
  background-image: linear-gradient(90deg, rgba(87, 208, 199, 0.4) 1.8px, transparent 0);
  background-size: 30px 15px, 15px 15px, 75px 75px, 75px 75px;
  border-radius: 4px 4px 0 0;
}
#c1-bottom {
  color: #fff;
  background-color: #39b7ac;
}
#c2 {
  background-color: #a9d96c;
  border-radius: 4px 4px 0 0;
}
#c2-bottom {
  color: #b5e378;
  background-color: #99c262;
}
#c3 {
  border-radius: 4px;
}

.weather-box {
  width: 100%;
  height: 100%;
  overflow: hidden;
  border-radius: 4px;
  .weather-top {
    display: flex;
    align-items: center;
    justify-content: space-around;
    height: 60%;
    font-size: 26px;
    color: #fff;
    background-color: #8175c7;
  }
  .weather-bottom {
    display: flex;
    align-items: center;
    justify-content: space-around;
    height: 40%;
    text-align: center;
    background-color: #fff;
  }
  .info {
    width: 33%;
  }
  .line {
    width: 1px;
    height: 60%;
    background: #ececec;
  }
}
.log-header {
  height: 56px;
  padding-left: 10px;
  font-weight: 600;
  line-height: 1.8;
  background-color: #fff;
  .log-title-zn {
    font-size: 16px;
    color: #43b1a5;
  }
  .log-title-en {
    font-size: 12px;
    color: #b2bcd0;
  }
}

.log-card {
  height: calc(100% - 56px);
  overflow: auto;
}
</style>
